package cc.uncarbon.module.appapi.service;


import cc.uncarbon.framework.core.context.UserContextHolder;
import cc.uncarbon.module.appapi.mapper.BookCategoryMapper;


import cc.uncarbon.module.appapi.model.request.BookCategoryInsertOrUpdateDTO;
import cc.uncarbon.module.appapi.model.response.BookCategoryBO;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yanrumiao.entity.BookCategoryEntity;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 数据表注释
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class BookCategoryService {

    private final BookCategoryMapper bookCategoryMapper;


    public Long create(BookCategoryInsertOrUpdateDTO dto) {
        BookCategoryEntity entity = new BookCategoryEntity();
        BeanUtil.copyProperties(dto, entity);
        entity.setUserId(UserContextHolder.getUserContext().getUserId());
        bookCategoryMapper.insert(entity);
        return entity.getId();
    }

    public List<BookCategoryBO> getBookCategoryList() {
        LambdaQueryWrapper<BookCategoryEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BookCategoryEntity::getUserId, UserContextHolder.getUserContext().getUserId());

        List<BookCategoryEntity> entityList = bookCategoryMapper.selectList(wrapper);
        List<BookCategoryBO> list = new ArrayList<>(entityList.size());

        entityList.forEach(entity -> list.add(this.entity2BO(entity)));
        return list;

    }

    public BookCategoryBO entity2BO(BookCategoryEntity entity) {
        BookCategoryBO categoryBO = new BookCategoryBO();
        BeanUtil.copyProperties(entity, categoryBO);
        return categoryBO;
    }

    public Long updata(BookCategoryInsertOrUpdateDTO dto) {

        BookCategoryEntity entity = new BookCategoryEntity();
        BeanUtil.copyProperties(dto, entity);
        bookCategoryMapper.updateById(entity);
        return entity.getId();
    }
}
